'''
coding:utf-8
@Software:PyCharm
@Time:2022/7/7 14:55
@Author:liuyi
'''
import random
import string
import sys
from time import sleep

# 角色列表
role_list = []
# 房间列表
room_list = []
# 装备列表
equipment_dict = {"防弹衣": 5}


# 角色
class Role:

    def __init__(self, username, sex, blood_volume=100, equipment=None, room=None):
        """
        :param username:名字
        :param sex:性别
        :param blood_volume:血量
        :param equipment:装备 列表
        :param room:房间
        """
        self.username = username
        self.sex = sex
        self.blood_volume = blood_volume

        # 装备
        self.equipment = equipment
        # if equipment is None:
        #     self.equipment = equipment
        # else:
        #     if equipment in equipment_dict.keys():
        #         self.equipment = equipment
        #     else:
        #         print("装备不存在")
        #         self.equipment = None

        # 房间
        if room is None:
            self.room = room
        else:
            if room.estimate_room_number_people():
                self.room = room
            else:
                print("房间人数已满")
                self.room = None
        sleep(3)


# 房间
class Room:

    def __init__(self, room_name, room_people, room_number_people=0):
        """
        :param room_name:房间名称
        :param room_number_people:房间人数，最多两人
        :param room_people : 房间角色列表
        """
        self.room_name = room_name
        self.room_number_people = room_number_people
        self.room_people = room_people

    # 判断房间人数
    def estimate_room_number_people(self):
        if 0 <= self.room_number_people <= 2:
            self.room_number_people += 1
            return True
        else:
            return False


class Util:
    # 初始化界面
    def interface(self):
        print("{:*^75}".format("欢迎进入回合制对战小游戏"))
        print("\t\t1.角色操作")  # 查看角色  创建角色    列出全部角色
        print("\t\t2.查看可选装备")
        print("\t\t3.房间操作")  # 查看房间  创建房间    列出全部房间
        print("\t\t4.开始游戏")  # 选择房间

    # contre == 1  角色操作界面
    def __role_operation_interface(self):
        print("{:*^80}".format("角色操作"))
        print("\t\t1.查看全部角色")
        print("\t\t2.查看角色信息")
        print("\t\t3.创建角色")
        print("\t\t4.角色加入房间(不存在加入,存在修改)")
        print("\t\t5.返回上一级")

    # contre == 1 具体操作
    def role_operation(self):
        while True:
            self.__role_operation_interface()
            contre = input("请输入你的选择:")
            if contre.isdigit():
                contre = int(contre)
                if contre == 1:  # 查看角色
                    self.__show_all_role()
                elif contre == 2:  # 查看指定角色详细信息
                    a = self.__specify_role_details()
                    if a == 1:
                        pass
                    elif a == 2:
                        return 5
                elif contre == 3:  # 创建角色
                    username = input("请输入你的名字(q返回上一级):")  # 名字
                    if username.lower() == "quit":
                        return 5
                    elif username.lower() == "q":
                        continue
                    sex = self.__judge_sex_legal()  # 性别
                    if sex == 1:
                        continue
                    elif sex == 2:
                        return 5
                    equipment = self.__input_equipment()  # 装备
                    if equipment == 1:
                        continue
                    elif equipment == 2:
                        return 5
                    room = self.__initialize_room(username)  # 房间
                    if room == 1:
                        continue
                    elif room == 2:
                        return 5
                    # 创建角色
                    temporary_user = self.__establish_role()
                    temporary_user = Role(username=username, sex=sex, equipment=equipment, room=room)
                    role_list.append(temporary_user)
                elif contre == 4:  # 角色加入房间  已经创建的角色
                    contre_4 = self.__join_or_modification()
                    if contre_4 == 1:
                        continue
                    elif contre_4 == 2:
                        return 5
                elif contre == 5:  # 返回上一级
                    return 4
                else:
                    print("你的输入有误，请重新输入")
            elif contre.lower() == "quit":  # 退出
                return 5
            else:
                print("你的输入有误，请重新输入")

    # 查看全部角色
    def __show_all_role(self):
        print("{:*^80}".format("全部角色"))
        print("{:^20}".format("角色"))
        for i in role_list:
            print("{:^20}".format(i.username))

    # 查看指定角色详细信息
    def __specify_role_details(self):
        # print("{:^10}{:^10}{:^10}{:^20}{:^10}".format("名字","性别","血量","装备","房间"))
        while True:
            bool = True
            username = input("请输入要查看角色的名称(q返回上一级):")
            if username.lower() == "q":
                return 1
            elif username.lower() == "quit":
                return 2
            for i in role_list:
                if username == i.username:
                    bool = False
                    conut_str = ""
                    room = ""
                    if i.equipment == []:
                        conut_str = "无"
                    else:
                        for j in i.equipment:
                            conut_str = conut_str + "、" + str(j)
                        conut_str = conut_str[1:]
                    # print(type(i.equipment))
                    if i.room == None:
                        room = "无"
                    print("{:^10}{:^10}{:^10}{:^20}{:^10}".format("名字", "性别", "血量", "装备", "房间"))
                    print("{:^10}{:^13}{:^10}{:^20}{:^10}".format(i.username, i.sex, i.blood_volume, conut_str, room))
            if bool:
                print("角色不存在")

    # 判断性别输入是否合法
    def __judge_sex_legal(self):
        while True:
            sex = input("请输入你的性别(q返回上一级):")
            if sex == "男" or sex == "女":
                return sex
            elif sex.lower() == "q":
                return 1
            elif sex.lower() == "quit":
                return 2
            else:
                print("你的输入有误")

    # 用户持续接收装备输入
    def __input_equipment(self):
        tmp = []
        while True:
            test_cont = input("是否需要添加装备(yes/no)(q返回上一级):")
            if test_cont.lower() == "yes":
                break
            elif test_cont.lower() == "no":
                return tmp
            elif test_cont.lower() == "q":
                return 1
            elif test_cont.lower() == "quit":
                return 2
            else:
                print("输入有误，请重新输入")
                continue
        while True:
            equipment = input("请输入给该用户添加的装备(q返回上一级,yes确认):")
            if equipment in equipment_dict.keys():
                tmp.append(equipment)
            elif equipment.lower() == "q":
                return 1
            elif equipment.lower() == "quit":
                return 2
            elif equipment.lower() == "yes":
                return tmp
            else:
                print("装备不存在")

    # 用于决定是否初始化进入房间
    def __initialize_room(self, username):
        while True:
            test_cont = input("是否要初始化角色房间(yes/no)(q返回上一级):")
            if test_cont.lower() == "yes":
                break
            elif test_cont.lower() == "no":
                return None
            elif test_cont.lower() == "quit":
                return 2
            elif test_cont.lower() == "q":
                return 1
            else:
                print("输入有误，请重新输入")
                continue
        while True:
            initialize_room = input("请输入要加入的房间名(q返回上一级):")
            for i in room_list:
                if initialize_room == i.room_name:
                    i.room_number_people += 1
                    i.room_people.append(username)
                    return initialize_room.lower()
                elif initialize_room.lower() == "q":
                    return 1
                elif initialize_room.lower() == "quit":
                    return 2
                else:
                    print("房间不存在")

    # 用于创建角色的时候生成随机字符串
    def __random_str(self):
        n = 0
        conut_str = ""
        temporary_str = string.ascii_lowercase
        while n < 4:
            conutre = random.randint(0, 25)
            conut_str = conut_str + temporary_str[conutre]
            n += 1
        return conut_str

    # 用于创建角色
    def __establish_role(self):
        while True:
            temporary_str = self.__random_str()
            if not temporary_str in role_list:
                return temporary_str
            else:
                continue

    # 用于该已经创建的角色加入房间或者修改房间
    def __join_or_modification(self):
        while True:
            username = input("请输入一个角色(q返回上一级):")
            if username.lower() == "q":
                return 1
            elif username.lower() == "quit":
                return 2
            bool = True
            new_username = ""
            for i in role_list:
                if i.username == username:
                    new_username = i
                    bool = False
            if bool:
                print("用户不存在")
                sleep(1)
                continue
            room = input("请输入要加入的房间名:")
            if room.lower() == "q":
                return 1
            elif room.lower() == "quit":
                return 2
            bool_room = True
            for i in room_list:
                if room == i.room_name:
                    bool_room = False
                    if i.room_number_people < 2:
                        i.room_number_people += 1
                        new_username.room = room
                        i.room_people.append(username)
                        print("成功！")
                        sleep(1)
                    else:
                        print("房间人数已满")
                        sleep(1)
            if bool_room:
                print("房间不存在")
                sleep(1)

    # contre == 2
    def equipment_list(self):
        print("{:*^80}".format("装备列表"))
        print("{:^20}{:^20}".format("装备名称", "用处"))
        for i, j in equipment_dict.items():
            print("{:^20}{:^20}".format(i, j))
        sleep(5)

    # contre == 3 房间操作界面
    def __room_operation_interface(self):
        print("{:*^80}".format("房间操作"))
        print("\t\t1.查看全部房间")
        print("\t\t2.查看房间信息")
        print("\t\t3.创建房间")
        print("\t\t4.返回上一级")

    # contre == 3 具体操作
    def room_operation(self):
        while True:
            self.__room_operation_interface()
            contre_3 = input("请输入你的选择:")
            if contre_3.isdigit():
                contre_3 = int(contre_3)
                if contre_3 == 1:  # 查看全部房间信息
                    self.__examine_all_room()
                elif contre_3 == 2:  # 查看指定房间信息
                    assign_room = self.__examine_assign_room()
                    if assign_room == 4:
                        continue
                    elif assign_room == 5:
                        return 5
                elif contre_3 == 3:  # 创建房间
                    establish_room = self.__establish_room()
                    if establish_room == 4:
                        continue
                    elif establish_room == 5:
                        return 5
                elif contre_3 == 4:  # 返回上一级
                    return 4
                else:
                    print("你输入的有误，请重新输入")
            elif contre_3.lower() == "quit":
                return 5
            else:
                print("你输入的有误，请重新输入")

    # 查看全部房间信息
    def __examine_all_room(self):
        print("{:*^79}".format("全部房间信息"))
        print("{:^20}{:^20}".format("房间名", "房间人数"))
        for i in room_list:
            print("{:^20}{:^20}".format(i.room_name, i.room_number_people))
        sleep(3)

    # 查看指定房间信息
    def __examine_assign_room(self):
        while True:
            room_name = input("请输入你要查看的房间名(q返回上一级):")
            if room_name.lower() == "q":
                return 4
            elif room_name.lower() == "quit":
                return 5
            bool = True
            for i in room_list:
                if room_name == i.room_name:
                    bool = False
                    print("{:^20}{:^20}".format("房间名", "房间人数"))
                    print("{:^20}{:^20}".format(i.room_name, i.room_number_people))
            if bool:
                print("房间不存在")

    # 创建房间
    def __establish_room(self):
        while True:
            room = input("请输入要创建房间的房间名(q返回上一级):")
            if room.lower() == "q":
                return 4
            elif room.lower() == "quit":
                return 5
            elif room.lower() == "":
                print("不能为空")
            bool = True
            for i in room_list:
                if room == i.room_name:
                    bool = False
                    print(f"{room}已存在")
            if bool:
                room_list.append(Room(room, room_people=[]))
                print("创建成功！")
                sleep(1)

    # contre = 4    开始游戏
    def begin_game(self):
        while True:
            begin_room = input("请输入开始游戏的房间名:")
            if begin_room.lower() == "q":
                return 1
            elif begin_room.lower() == "quit":
                return 2
            for i in room_list:
                if begin_room == i.room_name:
                    if i.room_number_people == 2:
                        num = 0
                        for j in i.room_people:
                            if j in [x.username for x in role_list]:
                                num += 1
                        if num == 2:
                            self.__game_course(i)
                        else:
                            print("房间角色异常")
                    else:
                        print("房间人数不足无法进行游戏")
                else:
                    print("房间不存在")

    # 游戏过程
    def __game_course(self, begin_room):
        # 获取角色对象
        username_one = ""
        username_two = ""
        for i in role_list:
            if i.username == begin_room.room_people[0]:
                username_one = i
            elif i.username == begin_room.room_people[1]:
                username_two = i
        # 获取血量
        username_one_blood = username_one.blood_volume
        username_two_blood = username_two.blood_volume
        # print(username_two_blood,username_one_blood)
        # 获取一次扣血值
        username_one_blood_deduct = 10
        username_two_blood_deduct = 10
        if username_one.equipment == None:
            # username_one_blood_deduct = 10
            pass
        else:
            # print(username_one.equipment)
            for i in username_one.equipment:
                for a, b in equipment_dict.items():
                    if i == a:
                        username_one_blood_deduct -= b
        if username_two.equipment == None:
            pass
        else:
            for i in username_two.equipment:
                for a, b in equipment_dict.items():
                    if i == a:
                        username_two_blood_deduct -= b
        while True:
            conut_num = random.randint(0, 1)
            if conut_num == 0:
                username_one_blood -= username_one_blood_deduct
                if username_one_blood <= 0:
                    print(f"{username_two.username}获胜")
                    break
            elif conut_num == 1:
                username_two_blood -= username_two_blood_deduct
                if username_two_blood <= 0:
                    print(f"{username_one.username}获胜")
                    break


if __name__ == '__main__':
    util = Util()
    while True:
        util.interface()
        contre = input("请输入你的选择:")
        if contre.isdigit():  # 判断是否是数字
            contre = int(contre)
            if contre == 1:  # 角色操作
                contre_1 = util.role_operation()
                if contre_1 == 4:
                    continue
                elif contre_1 == 5:
                    break
            elif contre == 2:  # 操作可选装备
                util.equipment_list()
            elif contre == 3:  # 房间操作
                contre_3 = util.room_operation()
                if contre_3 == 4:
                    continue
                elif contre_3 == 5:
                    break
            elif contre == 4:  # 开始游戏
                game = util.begin_game()
                if game == 1:
                    continue
                elif game == 2:
                    break
            else:
                print("你的输入有误，请重新输入")
                sleep(3)
        elif contre.lower() == "quit":
            print("退出")
            break
        else:
            print("你的输入有误，请重新输入")
            sleep(3)
